package rd.hardcore.engine.datas.streamBoard 
{
	import air.update.events.DownloadErrorEvent;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import rd.hardcore.engine.datas.StreamBoardData;
	
	/**
	 * ...
	 * @author Fazermokeur
	 */
	public class StreamBoardState extends EventDispatcher
	{
		//	Fichier créé sans data
		private var nwbie		: Boolean = true;
		private var saveFile	: File;
		private var _boards 	: Vector.<StreamBoardData>;
		
		public function StreamBoardState(v:Vector.<StreamBoardData>) 
		{
			_boards = v;
			if (!_boards) {
				buildBoard();
				return;
			}
			saveFile = File.applicationStorageDirectory.resolvePath("boards/boardState.xml");
			if (saveFile.exists) {
				nwbie = false;
				var fs:FileStream = new FileStream();
				var f:File = saveFile;
				fs.open(f, FileMode.READ);
				
				try {
					var stateXmlString:String 	= fs.readUTFBytes(fs.bytesAvailable);
					var stateXMLList:XMLList = new XMLList(stateXmlString);
					
					var len:int = _boards.length;
					var node:XML;
					for each(node in stateXMLList.board) {
						for (var i:int = 0; i < len; i++) {
							if (_boards[i].id == node.@id) {
								var bool:Boolean = (node.@state == "true")?true:false;
								_boards[i].active = bool;
								break;
							}
						//<board id=\"" + sbd.id + "\" state=\"" + sbd.active + "\" >"+sbd.navMediaURL+"</board>
						}
					}
					
				}catch (e:Error) {
					trace(e.errorID + " :: " + e.message);
				}
				
				fs.close();
			}else {
				setState();
			}
		}
		
		private function buildBoard():void
		{
			_boards = new Vector.<StreamBoardData>();
			
			saveFile = File.applicationStorageDirectory.resolvePath("boards/boardState.xml");
			if (saveFile.exists) {
				
				var fs:FileStream = new FileStream();
				var f:File = saveFile;
				fs.open(f, FileMode.READ);
				
				try {
					var stateXmlString:String 	= fs.readUTFBytes(fs.bytesAvailable);
					var stateXMLList:XMLList = new XMLList(stateXmlString);
					
					var len:int = _boards.length;
					var node:XML;
					var sData:StreamBoardData;
					for each(node in stateXMLList.board) {
						
						sData = new StreamBoardData(node.@id, node.@label);
						sData.buildDatas(new StreamBoardSynchroEvent(StreamBoardSynchroEvent.STREAMBOARD_SYNCHONIZED, node.@id));
						sData.active = node.@state;
						_boards.push(sData);
					}
					
				}catch (e:Error) {
					trace(e.errorID + " :: " + e.message);
				}
				
				fs.close();
			}
		}
	
		public function getDataVector():Vector.<StreamBoardData>
		{
			return _boards;
		}
		
		
		private function createNode(sbd:StreamBoardData):XML
		{
			var data:XML = new XML("<board id=\"" + sbd.id + "\" label=\""+sbd.labelName+"\" state=\"" + sbd.active + "\"><![CDATA["+sbd.preview+"]]></board>");
			//data.appendChild(data);
			return data;
		}
		
		public function setState():void
		{
			var datas:XML = generateStateXML();
			
			/*	WRITE	*/
			var fs:FileStream = new FileStream();
			var f:File = saveFile;
			
			fs.open(f, FileMode.WRITE);
			
			try {
				fs.writeUTFBytes(datas);
			}catch (e:Error) {
				trace(e.errorID + " :: " + e.message);
			}
			
			fs.close();
		}
		
		public function generateStateXML():XML
		{
			var states:XML = <data></data>;
			var len:int = _boards.length;
			for (var i:int = 0; i < len; i++) 
			{
				states.appendChild(createNode(_boards[i]));
			}
			return states;
		}
		
		public function stateChange(name:String):Boolean
		{
			var len:int =  _boards.length;
			for (var i:int = 0; i < len; i++) 
			{
				if (_boards[i].id == name) {
					_boards[i].active = !_boards[i].active;
					return _boards[i].active;
				}
			}
			return false;
		}
		
	}

}